#!/bin/sh

#
# Test writing a resource to an encrypted disk
#
# Note: fwup and dmcrypt pad blocks differently, so test files need to be
# multiples of 512.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource subdir/TEST {
        host-path = "${TESTFILE_1K}"
}

task complete {
        on-resource subdir/TEST { raw_write(1, "cipher=aes-cbc-plain", "secret=\\\${SECRET}") }
}
EOF

# Run ./169_disk_crypto_aes_cbc_plain-generate.sh
base64_decodez >$WORK/expected.img <<EOF
H4sIAGdny10AA+3UCzcVBgDA8bzJipDSgx5LiUhFlmvGTUPH42qVajXUpVCppLK4yZEjE0s5KBOV
QietpJQ8ijhRCzfWKa3aFburkISL7GNs5+z/+xC/MWPwf7aoNiYj1bSvf0P4+LWK9UdqAopHbTzc
pC5Cr+67G+NdG19XDr+QSwyN7txecd0rXLQzXCNcWd+hdF6ovZKK2YmL4nrzho5epd2niodt//qz
9xdFettciU3ElfuX1igO929KvHzYK88jY0jnZ3W5s673b2M1Is3MG5MNi7WkDssrhOuCraIE4lMO
8w1WB87b66eX550z5NISst/SO+hS85vZqqkNSv3RYf4phruqBO9ta9/tdkyw0WzwkmRp71jjEt+k
aRh5pk3kU254JdTCW6Z9+xuxUVdO/GC1257YLeVHqvqytVMWSn8K+kE29ZVRZcvwubDV/l0fvVy6
VYb1jadNChcveala2OPpXOM2YfuyWfczo60vGMXv716zPUFXe6F/mqzT2F+q0G9MNmgXqn+bUhhp
3O0e9DZbUtSVmS77pN6V49ykfKZ8rG2Imuk499x88wKfaSOX7inlT1vzZdIs7we7js9Z4uM62WTS
wazGeToCmTx3hqmv4fls9UCFLMQir7fML33Ya/GnALmZqHAo683jsviCDYYtkvlqljeMKxZeqJ7u
f9tO5Ciobnhw9tOHPFexu1ZL4xjFJp+BnIOZCe+OLt53yOlBzDI9P+ujH0evBkttp3soUvvm1uq4
HLNfnNhSYGKpHKvVY56xTzNqwL8iOW5LQe/nta80ZVF23V8XCaoHLY3LwmMDy4sK/kh665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dd9cded6aab2897568cc856" $FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete

cmp $WORK/expected.img $IMGFILE

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
